/**
 * Copyright ©2016-2020 northkingbpo Corporation, All Rights Reserved
 * http://www.northkingbpo.net/
 */
package profile.hujia.algorithm.simulate;

import lombok.Data;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

/**
 * class info
 *
 * @author hujia
 * @date 2019-07-15 09:06
 */
@Data
public class ExchangeProxy {
    ConstraintCondition constraintCondition;
    InitialCondition initialCondition;

    public ExchangeProxy(ConstraintCondition constraintCondition) {
        this.constraintCondition = constraintCondition;
        initialCondition = new InitialCondition();
    }

    public void run() {
        initialCondition.setKeyNumber(constraintCondition.getKeyNumber());
        initialCondition.setPeopleNumber(constraintCondition.getPeopleNumber());
        initialCondition.setTaskNumber(constraintCondition.getTaskNumber());
        setCapacity();
        setPeopleAbility();
        setTaskQueue();
        setPeopleQueue();
    }

    private void setPeopleAbility() {
        HashMap<Integer, List<Integer>> peopleAbility = new HashMap<>();
        for (int i = 0; i < constraintCondition.getPeopleNumber(); ++i) {
            List<Integer> list = new ArrayList<>();
            for (int j = 0; j < constraintCondition.getNumberOfPeopleAbility()[i]; ++j) {
                list.add(constraintCondition.getPeopleAbilitySpecific()[i][j]);
            }
            peopleAbility.put(i, list);
        }
        initialCondition.setPeopleAbility(peopleAbility);
    }

    private void setPeopleQueue() {
        HashMap<Integer, List<Integer>> peopleQueue = new HashMap<>();
        for (int i = 0; i < constraintCondition.getKeyNumber(); ++i) {
            List<PeopleAbility> list = new ArrayList<>();
            for (int j = 0; j < constraintCondition.getNumberOfPeopleForEachKey()[i]; ++j) {
                PeopleAbility peopleAbility = new PeopleAbility();
                int people = constraintCondition.getKeyOfPeopleSpecific()[i][j]-1;
                peopleAbility.setId(people);
                peopleAbility.setAbilityNumber(constraintCondition.getNumberOfPeopleAbility()[people]);
                list.add(peopleAbility);
            }
            list.sort(Comparator.comparing(PeopleAbility::getAbilityNumber));
            List<Integer> resultList = new ArrayList<>();
            for (PeopleAbility peopleAbility : list) {
                resultList.add(peopleAbility.getId());
            }
            peopleQueue.put(i,resultList);
        }
        initialCondition.setPeopleQueue(peopleQueue);
    }

    private void setTaskQueue() {
        HashMap<Integer, List<Integer>> taskQueue = new HashMap<>();
        for (int i = 0; i < constraintCondition.getKeyNumber(); ++i) {
            List<Integer> list = new ArrayList<>();
            for (int j = 0; j < constraintCondition.getNumberOfTaskForEachKey()[i]; ++j) {
                list.add(constraintCondition.getKeyOfTaskSpecific()[i][j]);
            }
            taskQueue.put(i, list);
        }
        initialCondition.setTaskQueue(taskQueue);
    }

    private void setCapacity() {
        HashMap<Integer, Integer> peopleCapacity = new HashMap<>();
        for (int i = 0; i < constraintCondition.getPeopleCapacity().length; ++i) {
            peopleCapacity.put(i, constraintCondition.getPeopleCapacity()[i]);
        }
        initialCondition.setPeopleCapacity(peopleCapacity);
    }
}
